<?php

uses('sanitize');

class PagesController extends AppController{
	var $name = 'Pages';
	var $helpers = array('Html2', 'Form', 'Javascript', 'Partial', 'ajax');
	var $components = array('RequestHandler', 'Textual');
	var $paginate = array('limit' => 5, 'order' => array('Page.created' => 'desc'));

//guest
	
	function index(){
		$this->Page->recursive = 0;
		$this->pageTitle = 'Last blogs';
		$this->set('pageTitle', $this->pageTitle);
		$this->set('rssLink', true);
		$this->params['url']['url'] = '/pages/'.$this->action;
	//RSS
		$isFeed = ife($this->RequestHandler->prefers('rss') == 'rss', true, false);
		if($isFeed){
			$this->set('channel', array('description' => 'todo:description from config'));
			$this->set('pages', $this->Page->findAll(array('Page.online' => '1', 'DATE(Page.created)' => '<= -!CURRENT_DATE()'), null, 'Page.created DESC', 20));
		}else{
	//standard
			//select pages : online and before today
			$this->set('pages', $this->paginate(array('Page.online' => 1, 'DATE(Page.created)' => '<= -!CURRENT_DATE()')));
		}
	}

	function view($slug = null) {
		$page = $this->Page->findBySlug($slug);
		if(!$slug || !$page) {
			$this->Session->setFlash('Page Not available.');
			$this->redirect('/pages/index');
		}
		
		$this->pageTitle = $page['Page']['title'];
		$this->set('page', $page);
		//$neighbours['prev'] = $this->Page->query('SELECT `Page`.`id`, `Page`.`title`, `Page`.`slug` FROM `pages` AS `Page` WHERE `Page`.`online` = 1 AND DATE(`Page`.`created`) <= -!CURRENT_DATE() AND DATE(`Page`.`created`) < DATE("'.$page['Page']['created'].'") ORDER BY `Page`.`created` DESC LIMIT 1');
		//$neighbours['next'] = $this->Page->query('SELECT `Page`.`id`, `Page`.`title`, `Page`.`slug` FROM `pages` AS `Page` WHERE `Page`.`online` = 1 AND DATE(`Page`.`created`) <= -!CURRENT_DATE() AND DATE(`Page`.`created`) > DATE("'.$page['Page']['created'].'") ORDER BY `Page`.`created` DESC LIMIT 1');
		//$neighbours = $this->Page->findNeighbours(null, 'Page.id', $page['Page']['id']);
		//$this->set('neighbours', $neighbours);
	}

	function category($slug = null){
		if(!$slug) {
			$this->Session->setFlash('Not available Category.');
			$this->redirect('/pages/index');
		}
		
		//RSS
		$isFeed = ife($this->RequestHandler->prefers('rss') == 'rss', true, false);
		if($isFeed){
			$pages = $this->Page->findAll(array('`Category`.`slug`' => $slug, 'Page.online' => '1', 'DATE(Page.created)' => '<= -!CURRENT_DATE()'), null, 'Page.created DESC', 20);
			$this->set('channel', array('description' =>  _('Category').' : '.$pages[0]['Category']['title']));
			$this->set('pages', $pages);
		}else{
			$cond = array('`Category`.`slug`' => $slug, 'Page.online' => '1', 'DATE(Page.created)' => '<= -!CURRENT_DATE()');
			$pages = $this->paginate('Page', $cond);
			$this->pageTitle = _('Category').' : '.$pages[0]['Category']['title'];
			$this->set('pages', $pages);
			$this->set('slug', $slug);
			
			$this->set('pageTitle', $this->pageTitle);
			$this->set('rssLink', true);
		}
		
		$this->render('index');
	}
	
//comments : ajaxAdd;
	function commentAdd($page_id = null){
		if(!isset($page_id)){
			die('<p class="error_message">no comment allowed</p>');
		}
		
		if($this->RequestHandler->isAjax()){
	
			if(!empty($this->data)){
				$this->data['Comment']['ip'] = $this->RequestHandler->getClientIP();
				$this->data['Comment']['page_id'] = $page_id;
				
				$this->cleanUpFields();
			
				if($this->Page->Comment->save($this->data)) {
					$this->Session->setFlash('The Comment has been saved');
					$this->set('page_id', $this->data['Comment']['page_id']);
					$this->set('closeajax', true);
				
				} else {
					$this->Session->setFlash('Please correct errors below.');
				
				}
			}
		}
	}
	
	function comments_list($page_id = null){
		if(!isset($page_id)){
			die('id not specified');
		}
		if($this->RequestHandler->isAjax()){			
			$this->set('page', $this->Page->findById($page_id));
			
			//this render element in id=comments_list div
			$this->render('comments_list', 'ajax', VIEWS.'elements/pages/_comments_list.thtml');
		}
	}


//admin functions

	function admin_index(){
		$this->pageTitle = 'Admin Pages';
		$this->set('pages', $this->paginate());
		
	}
	
	function admin_add() {		
		
		$this->__admin_init();
		
		if($this->RequestHandler->isPost()){
		
			$this->__sanitize();
			
			if($this->Page->save($this->data)){
				$this->Session->setFlash('The Page has been saved');
				$this->redirect('/admin/pages/index');
			}else{
				$this->Session->setFlash('Please correct errors below.');
			}
		}else{
			$this->render();
		}
	}

	function admin_edit($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Page');
			$this->redirect('/admin/pages/index');
		}
		//$this->cleanUpFields('Page');
		$this->__admin_init();
		
		if($this->RequestHandler->isPost()){
			
			$this->__sanitize();

			if($this->Page->save($this->data)) {
				$this->Session->setFlash('The Page has been saved');
				$this->redirect('/admin/pages');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}else{
			$this->data = $this->Page->read(null, $id);
		}		
	}

	function admin_delete($id){
		//if($this->RequestHandler->isDelete()){ //voir html pour restfull
		if($this->RequestHandler->isPost()){
			//$page = $this->Page->findById($id, 'category_id', 0);
			//$this->data['Page']['category_id'] = $page['Page']['category_id'];
			
			$this->Page->del($id);
			$this->Session->setFlash('The Page with id : '.$id.' has been deleted.');
		}else{
			$this->Session->setFlash('Error');
		}
		$this->redirect('/admin/pages');
	}
	
	/* private */
	
	function __admin_init() {
		$this->set('users', $this->Page->User->select_users());		
		$this->set('categories', $this->Page->Category->select_categories());
	}
	
	function __sanitize(){
		$san = new Sanitize();
		
		$this->data['Page']['title'] = $san->stripAll($this->data['Page']['title']);
		$this->data['Page']['slug'] = $san->paranoid($this->Textual->remplace_char($this->data['Page']['slug'], null, 20), array('-'));
		//supposed admin don't hack himself
		//$this->data['Page']['content'] = $san->escape($this->data['Page']['content']);
		
		//correct bug error about value of min
		if($this->data['Page']['created_min'] < 10) $this->data['Page']['created_min'] = '0'.$this->data['Page']['created_min'];
		$this->data['Page']['created'] = $this->data['Page']['created_year'].'-'.$this->data['Page']['created_month'].'-'.$this->data['Page']['created_day'].' '.$this->data['Page']['created_hour'].':'.$this->data['Page']['created_min'].':00';
		
		//debug($this->data['Page']['created']);
		
	}
}
?>
